package com.dd.admin.business.member.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dd.admin.common.aop.operationLog.aop.OperLog;
import com.dd.admin.common.aop.operationLog.aop.OperType;
import com.dd.admin.common.exception.ApiException;
import com.dd.admin.common.security.SecurityUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.dd.admin.common.model.UpdateGroup;
import com.dd.admin.common.model.result.ResultBean;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotBlank;
import com.dd.admin.business.member.entity.Member;
import com.dd.admin.business.member.domain.MemberVo;
import com.dd.admin.business.member.domain.MemberDto;
import com.dd.admin.business.member.service.MemberService;

import java.util.List;

import org.springframework.web.bind.annotation.RestController;

import static com.dd.admin.business.pay.enmus.CashierExceptionEnum.MEMBER_ALREADY_BUILD;

/**
 * <p>
 * 会员 前端控制器
 * </p>
 *
 * @author 727869402@qq.com
 * @since 2024-05-21
 */
@Api(tags = "会员")
@RestController
public class MemberController {

    @Autowired
    MemberService memberService;

    @ApiOperation(value = "会员-分页列表")
    @ApiOperationSupport(order = 1)
    @GetMapping("/admin/member/page")
    public ResultBean<IPage<MemberVo>> page(MemberDto memberDto) {
        memberDto.setShopId(SecurityUtil.getLoginUser().getDeptId());
        IPage<MemberVo> pageInfo = memberService.selectMemberPage(memberDto);
        return ResultBean.success(pageInfo);
    }

    @ApiOperation(value = "会员-列表")
    @ApiOperationSupport(order = 2)
    @GetMapping("/admin/member/list")
    public ResultBean<List<MemberVo>> list(MemberDto memberDto) {
        memberDto.setShopId(SecurityUtil.getLoginUser().getDeptId());
        List<MemberVo> list = memberService.selectMemberList(memberDto);
        return ResultBean.success(list);
    }

    @ApiOperation(value = "会员-列表")
    @ApiOperationSupport(order = 2)
    @GetMapping("/admin/member/searchList")
    @OperLog(operModule = "会员查询",operType = OperType.QUERY,operDesc = "会员查询")
    public ResultBean<List<MemberVo>> searchList(MemberDto memberDto) {
        memberDto.setShopId(SecurityUtil.getLoginUser().getDeptId());
        memberDto.setLimit(10);
        List<MemberVo> list = memberService.selectMemberList(memberDto);
        return ResultBean.success(list);
    }

    @ApiOperation(value = "会员-添加")
    @ApiOperationSupport(order = 3)
    @PostMapping("/admin/member/add")
    @OperLog(operModule = "添加会员",operType = OperType.ADD,operDesc = "添加会员")
    public ResultBean<Member> add(@RequestBody @Validated MemberDto memberDto) {
        // 先根据手机号查询是否已存在会员
        LambdaQueryWrapper<Member> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Member::getMemberPhone, memberDto.getMemberPhone());
        Member existingMembers = memberService.getOne(queryWrapper);
        if (existingMembers != null) {
            // 如果存在，则返回错误信息，表示手机号已被注册
            throw new ApiException(MEMBER_ALREADY_BUILD);
        }
        Member member = BeanUtil.copyProperties(memberDto, Member.class);
        memberService.save(member);
        return ResultBean.success(member);
    }

    @ApiOperation(value = "会员-查询")
    @ApiOperationSupport(order = 4)
    @GetMapping("/admin/member/{memberId}")
    public ResultBean<MemberVo> get(@PathVariable @NotBlank String memberId) {
        Member member = memberService.getById(memberId);
        MemberVo memberVo = BeanUtil.copyProperties(member,MemberVo.class);
        return ResultBean.success(memberVo);
    }

    @ApiOperation(value = "会员-修改")
    @ApiOperationSupport(order = 5)
    @PostMapping("/admin/member/update")
    public ResultBean<Member> update(@RequestBody @Validated(UpdateGroup.class) MemberDto memberDto) {
        Member member = BeanUtil.copyProperties(memberDto, Member.class);
        memberService.updateById(member);
        return ResultBean.success(member);
    }

    @ApiOperation(value = "会员-删除")
    @ApiOperationSupport(order = 6)
    @GetMapping("/admin/member/delete/{memberId}")
    public ResultBean<Member> delete(@PathVariable @NotBlank String memberId) {
        Boolean b = memberService.removeById(memberId);
        return ResultBean.success(b);
    }
}
